package intro.databases.gtCarRental.ui;

import intro.databases.gtCarRental.GTCRActivity;
import intro.databases.gtCarRental.R;
import intro.databases.gtCarRental.ui.admin.Admin_Home;
import intro.databases.gtCarRental.ui.employee.Employee_Home;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends GTCRActivity {
	EditText username, password;

	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.login);
		username = (EditText) findViewById(R.id.editText_login_username);
		password = (EditText) findViewById(R.id.editText_login_password);
	}

	public void createAccount(View v) {
		Intent intent = new Intent(this, CreateAccount.class);
		startActivity(intent);
		this.finish();
	}

	public void login(View v) {
		if(!(username.getText().toString().equals("") || password.getText().toString().equals(""))) {
			this.application.currentUser = dbConn.getUserFromCredentials(username.getText().toString(), password.getText().toString());
			if(this.application.currentUser != null) {
				switch(application.currentUser.type) {
				case ADMIN:
					startAdmin();
					break;
				case EMPLOYEE:
					startEmployee();
					break;
				case MEMBER:
					startMember();
					break;
				}
				this.finish();
			} else {
				Toast.makeText(this, "Username and password not recognized", Toast.LENGTH_LONG).show();
			}
		} else {
			Toast.makeText(this, "Please input your login information", Toast.LENGTH_LONG).show();
		}
	}

	private void startAdmin() {
		Intent intent = new Intent(this, Admin_Home.class);
		startActivity(intent);
	}

	private void startEmployee() {
		Intent intent = new Intent(this, Employee_Home.class);
		startActivity(intent);
	}

	private void startMember() {
		Intent intent = new Intent(this, Home.class);
		startActivity(intent);
	}

}
